//
//  ECCustomSectionTapView.m
//  ERPChargeAccount2
//
//  Created by 刘思麟 on 2018/7/10.
//  Copyright © 2018年 历山大亚. All rights reserved.
//

#import "ECCustomSectionTapView.h"
#import <AudioToolbox/AudioToolbox.h>

@interface ECCustomSectionTapView(){
    NSMutableArray * titlesFrameArr;
}

@end

@implementation ECCustomSectionTapView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        titlesFrameArr = @[].mutableCopy;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat titlesPlaceHoldeHeight = 0;
    for (NSString * str in self.titles) {
        CGSize strSize = [str sizeForFont:[UIFont systemFontOfSize:self.fontSize] size:CGSizeMake(self.width, 0) mode:NSLineBreakByCharWrapping];
        titlesPlaceHoldeHeight += strSize.height + self.charSplit;
    }
    CGFloat startY = (self.height - titlesPlaceHoldeHeight) / 2;
    [titlesFrameArr removeAllObjects];
    for (int index = 0; index < self.titles.count; index ++) {
        @autoreleasepool{
            CATextLayer * textLayer = [CATextLayer layer];
            NSString * str = self.titles[index];
            textLayer.string = str;
            textLayer.fontSize = self.fontSize;
            textLayer.foregroundColor = ECCOLOR(0x333333).CGColor;
            textLayer.alignmentMode = kCAAlignmentCenter;
            textLayer.wrapped = YES;
            CGSize strSize = [str sizeForFont:[UIFont systemFontOfSize:self.fontSize] size:CGSizeMake(self.width, 0) mode:NSLineBreakByWordWrapping];
            CGRect textFrame = CGRectMake(0, startY, self.width, strSize.height + self.charSplit);
            textLayer.frame = textFrame;
            startY += textFrame.size.height;
            [self.layer addSublayer:textLayer];
            [titlesFrameArr addObject:[NSValue valueWithCGRect:textFrame]];
        }
    }
}

CGRect currentTitleFrame;
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    CGPoint locationPoint = [touch locationInView:self];
    WS(weakSelf, self);
    [titlesFrameArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGRect titleFrame = [obj CGRectValue];
        if(CGRectContainsPoint(titleFrame, locationPoint)){
            currentTitleFrame = titleFrame;
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(selectedAtIndex:)]) {
                [weakSelf.delegate selectedAtIndex:idx];
            }
            AudioServicesPlayAlertSound(1036);
        }
    }];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    CGPoint locationPoint = [touch locationInView:self];
    WS(weakSelf, self);
    [titlesFrameArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        CGRect titleFrame = [obj CGRectValue];
        if(CGRectContainsPoint(titleFrame, locationPoint) && !CGRectEqualToRect(titleFrame, currentTitleFrame)){
            currentTitleFrame = titleFrame;
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(selectedAtIndex:)]) {
                [weakSelf.delegate selectedAtIndex:idx];
            }
            AudioServicesPlayAlertSound(1036);
        }
    }];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    currentTitleFrame = CGRectZero;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if (!self.hidden) {
        CGRect useRect = CGRectMake(0, CGRectGetMinY([titlesFrameArr.firstObject CGRectValue]), self.width, CGRectGetMaxY([titlesFrameArr.lastObject CGRectValue]) - CGRectGetMinY([titlesFrameArr.firstObject CGRectValue]));
        if (CGRectContainsPoint(useRect, point)) {
            return self;
        }
    }
    return [super hitTest:point withEvent:event];
}

@end
